export function transForm({ series, ...args }) {
  // format all x into one array
  let xAxias = new Set()
  const labels = ['x']
  for (const curve of series) {
    labels.push(curve.name)
    const { data } = curve

    for (const point of data) {
      xAxias.add(point.x)
    }
  }

  // sort this array
  xAxias = Array.from(xAxias).sort((a, b) => a - b)

  // then assign y to array, if can not find, set null
  const result = []
  for (const x of xAxias) {
    const arr = [x]
    for (const curve of series) {
      const p = curve.data.filter(item => item.x === x)[0]
      arr.push(p && p.y ? p.y : null)
    }

    result.push(arr)
  }

  return {
    ...args,
    series: {
      list: result,
      label: labels
    }
  }
}

export function transFormToUPlot({ series, ...args }) {
  // format all x into one array
  const labels = [
    {
      id: 'x'
    }
  ]

  const bands = []

  const result = []

  function joinValuetoIndex(index, list) {
    for (let i = 0; i < result.length; i++) {
      const item = list.filter(s => s.curveIndex === i)[0]

      result[i].splice(index, 0, item ? item.value : null)
    }
  }

  function calculateMinIndex(val, lastMinIndex) {
    if (lastMinIndex > result[0].length) return result[0].length

    for (let i = lastMinIndex; i < result[0].length; i++) {
      if (val < result[0][i]) return i
    }

    return result[0].length
  }

  for (let curveIndex in series) {
    curveIndex = parseInt(curveIndex)
    const curve = series[curveIndex]
    // format curve label
    const labelNames = curve.isArea
      ? [curve.name + '-Min', curve.name + '-Max']
      : [curve.name]
    labels.push(...labelNames.map(item => ({ id: item, ...curve })))

    // if is area, format bands
    if (curve.isArea) {
      bands.push({
        show: true,
        series: [2 * curveIndex + 2, 2 * curveIndex + 1],
        fill: () => curve.color
      })
    }

    const { data } = curve
    // as first curve
    if (curveIndex === 0) {
      result.push(
        data.map(item => item.x),
        data.map(item => item.y)
      )

      if (curve.isArea) {
        result.splice(
          1,
          0,
          data.map(item => item.l)
        )
      }

      continue
    }

    const arrayIndex = curve.isArea ? curveIndex * 2 + 1 : curveIndex + 1

    // push a null list in result
    result.push(Array(result[0].length).fill(null))
    // if is Area
    if (curve.isArea) {
      result.push(Array(result[0].length).fill(null))
    }

    let lastMinIndex = 0

    for (const point of data) {
      let index = result[0].indexOf(point.x)
      // if at the x list
      if (index !== -1) {
        if (curve.isArea) {
          result[arrayIndex][index] = point.l
          result[arrayIndex + 1][index] = point.y
        } else {
          result[arrayIndex][index] = point.y
        }

        continue
      }

      // find out which index should to place the new number
      index = calculateMinIndex(point.x, lastMinIndex)

      lastMinIndex = index

      if (curve.isArea) {
        joinValuetoIndex(index, [
          { curveIndex: 0, value: point.x },
          { curveIndex: arrayIndex, value: point.l },
          { curveIndex: arrayIndex + 1, value: point.y }
        ])
      } else {
        joinValuetoIndex(index, [
          { curveIndex: 0, value: point.x },
          { curveIndex: arrayIndex, value: point.y }
        ])
      }
    }
  }

  // console.log(result)

  return {
    ...args,
    series: {
      list: result,
      label: labels,
      bands
    }
  }
}
